package com.techthinker.dreamBuilder.Activitites;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.techthinker.dreamBuilder.R;
import com.techthinker.dreamBuilder.Dictionary.DictionaryAdapter;
import com.techthinker.dreamBuilder.Dictionary.DictionaryAdapter.DictionaryViewHolder;
import com.techthinker.dreamBuilder.Dictionary.DictionaryDAL;
import com.techthinker.dreamBuilder.Dictionary.ListDAL;
import com.techthinker.dreamBuilder.Dictionary.WordDAL;
import com.techthinker.dreamBuilder.Entity.Dictionary;
import com.techthinker.dreamBuilder.Entity.Lists;
import com.techthinker.dreamBuilder.Entity.Word;
import com.techthinker.dreamBuilder.Utils.FileHelper;
import com.techthinker.dreamBuilder.Utils.XMLParser;

public class Dic_fromSDActivity extends Activity {

	private List<Dictionary> xmlDicFiles;
	private XMLParser dicParser;
	private ListView list_dic;
	private ImageButton ib_selectall;
	private ImageButton ib_import;
	private DictionaryAdapter dicAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dic_fromsd);

		dicParser = new XMLParser();
		xmlDicFiles = new ArrayList<Dictionary>();
		// initiate the xmlDicFiles
		scanXMLDic();

		dicAdapter = new DictionaryAdapter(this, R.layout.dic_list_item,
				R.id.tv_dic_name, xmlDicFiles);
		list_dic = (ListView) findViewById(R.id.lv_xmlDic);
		list_dic.setDivider(null);
		list_dic.setFocusable(false);
		list_dic.setTextFilterEnabled(true);
		list_dic.setAdapter(dicAdapter);

		ib_selectall = (ImageButton) findViewById(R.id.ib_dic_selectall);
		// change the CheckBoxes's states
		ib_selectall.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// remember to refresh the views' states
				list_dic.refreshDrawableState();
				int allItem = list_dic.getChildCount() - 2;
				for (int i = 1; i <= allItem; i++) {
					DictionaryViewHolder dvh = (DictionaryViewHolder) list_dic
							.getChildAt(i).getTag();
					dvh.select.setChecked(!dvh.select.isChecked());
				}
			}
		});

		ib_import = (ImageButton) findViewById(R.id.ib_dic_import);
		ib_import.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// remember to refresh the views' states
				list_dic.refreshDrawableState();
				int allItem = list_dic.getChildCount() - 2;
				for (int i = 1; i <= allItem; i++) {
					DictionaryViewHolder dvh = (DictionaryViewHolder) list_dic
							.getChildAt(i).getTag();
					Dictionary dic = (Dictionary) list_dic.getItemAtPosition(i);
					if (dvh.select.isChecked()) {
						if (dicIsInstalled(dic.getName(), dic.getVersion())) {

						} else {
							try {
								installDictionary(dicParser
										.parseXMLToDictionary(new FileHelper()
												.getInputStream(dvh.name
														.getText() + ".xml")));
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		});

	}

	// install the dictionary to database
	public void installDictionary(Dictionary dictionary) {
		List<Lists> tempLists = dictionary.getAllLists();
		List<Word> tempWords;
		// create a dictionary item
		{
			DictionaryDAL dicDAL = new DictionaryDAL(this);
			dicDAL.open();
			dictionary.setDictionary_id((int) dicDAL
					.createDictionary(dictionary));
			dicDAL.close();
			// create the related word table
			String dicName = dictionary.getName() + "_"
					+ dictionary.getVersion().replace(' ', '_');
			WordDAL wDAL = new WordDAL(this);
			wDAL.open();
			wDAL.createWordTableByDicName(dicName);
			wDAL.close();
		}
		for (Lists ls : tempLists) {
			tempWords = ls.getAllWords();
			// create a list item
			{
				ls.setDictionary(dictionary);
				ListDAL listDAL = new ListDAL(this);
				listDAL.open();
				ls.setList_id((int) listDAL.createLists(ls));
				listDAL.close();
			}
			for (Word wd : tempWords) {
				// create a word item
				{
					wd.setLists(ls);
					Dictionary notebook = new Dictionary();
					notebook.setDictionary_id(0);
					wd.setNotebook(notebook);
					WordDAL wordDAL = new WordDAL(this);
					wordDAL.open();
					wd.setWord_id((int) wordDAL.createWord(wd));
					wordDAL.close();
				}
			}
		}
	}

	// scan all the dictionary in the default directory
	public void scanXMLDic() {
		FileHelper fileHelper = new FileHelper();
		File[] files = fileHelper.listAllFiles();
		if (files != null) {
			xmlDicFiles.clear();
			// add a vacuum dictionary for header bar
			xmlDicFiles.add(new Dictionary());
			for (File f : files) {
				if (f.getName().endsWith("xml")) {
					// add the simple info of a dictionary to the xmlDicFiles
					try {
						xmlDicFiles.add(dicParser.simpleParseDicXML(fileHelper
								.getInputStream(f.getName())));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			// add a vacuum dictionary for bottom bar
			xmlDicFiles.add(new Dictionary());
		} else {
			// post a dialog to alert
		}
	}

	// check if the dictionary is installed
	public boolean dicIsInstalled(String dicName, String verName) {
		boolean dicState;
		DictionaryDAL dicDAL = new DictionaryDAL(this);
		dicDAL.open();
		dicState = dicDAL.dicIsInstalled(dicName, verName);
		dicDAL.close();
		return dicState;
	}

}
